package com.xiaozhengkeji.mc.plugins.cdkey.base;


import com.xiaozhengkeji.mc.plugins.cdkey.config.FileConfig;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * 数据持久化操作类
 */
public class BaseFactory {

    //获取礼物
    public List<String> getCB(String tab) {
        YamlConfiguration BaseYml = YamlConfiguration.loadConfiguration(new File(FileConfig.Plugin_File_CommandsBase));
        return BaseYml.getStringList(tab);
    }

    //将CDKey写入文件
    public void expText(List<String> cdks) {
        String path = FileConfig.Plugin_DateBase_Path + "/" + String.valueOf(new Date().getTime()) + ".txt";
        StringBuffer buf = new StringBuffer();
        cdks.forEach(cdk -> buf.append(cdk + "\n"));
        File file = new File(path);
        try {
            file.createNewFile();
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true)));
            out.write(buf + "\r\n");
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
